#include "createmap.h"
#include "ui_createmap.h"

CreateMap::CreateMap(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CreateMap)
{
    ui->setupUi(this);
    init();
}

CreateMap::~CreateMap()
{
    delete ui;
}

void CreateMap::init()
{
    width=10;
    nx=0,ny=0;
    global_width=false;
    endx=-1,endy=-1;
    lastNum=0;
    path.clear();
}

void CreateMap::keyPressEvent(QKeyEvent *ev)
{
    if(ev->key()==Qt::Key_W)
    {
        width++;
        this->update();
    }
    else if(ev->key()==Qt::Key_N)
    {
        width--;
        if(width<1)
            width=1;
        this->update();
    }
    else if(ev->key()==Qt::Key_Z&&!ev->isAutoRepeat())
    {
        backPoints();
        this->update();
    }
    else if(ev->key()==Qt::Key_Q&&!ev->isAutoRepeat())
    {
        lastNum=path.size()-10;
        if(lastNum<0)
            lastNum=0;
        backPoints();
        this->update();
    }
}

void CreateMap::paintEvent(QPaintEvent *)
{
    QPainter painter(this); // this 是 的指针
    QPen pen;
    pen.setWidth(width);
    pen.setBrush(Qt::black);
    pen.setCapStyle(Qt::RoundCap);
    pen.setJoinStyle(Qt::RoundJoin);
    painter.setPen(pen);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.drawLine(50,0,50,50);
    if(path.size()>1)
    {
        for(int i=1;i<path.size();i++)
        {
            if(global_width)
            {
                pen.setWidth(path[i].width);
                painter.setPen(pen);
            }
            painter.drawLine(path[i-1].sx,path[i-1].sy,path[i].sx,path[i].sy);
        }
    }
    if(endx!=-1&&endy!=-1)
    {
        pen.setWidth(2);
        pen.setBrush(Qt::green);
        painter.setPen(pen);
        painter.drawLine(endx,endy-width/2,endx,endy+width/2);
    }
}

void CreateMap::backPoints()
{
    while(1)
    {
        if(path.size()<=lastNum||path.size()==0)
            break;
        else
            path.pop_back();
    }
}

void CreateMap::addPoints(int x, int y)
{
    Block b;
    b.sx=x;
    b.sy=y;
    b.width=width;
    path.push_back(b);

}

void CreateMap::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        lastNum=path.size();
        lastx=nx,lasty=ny;
        nx=event->x(),ny=event->y();
        addPoints(event->pos().x(),event->pos().y());
        this->update();
    }
    else if(event->button() == Qt::RightButton)
    {
        endx=event->x(),endy=event->y();
        this->update();
    }
}

void CreateMap::mouseReleaseEvent(QMouseEvent *event)
{
}


void CreateMap::mouseMoveEvent(QMouseEvent * event)
{
    lastx=nx,lasty=ny;
    nx=event->x(),ny=event->y();
    addPoints(event->x(),event->y());
    this->update();

}

void CreateMap::on_pushButton_2_clicked()
{
    if(global_width)
        global_width=false;
    else
        global_width=true;

    if(global_width)
        ui->pushButton_2->setText("LW");
    else
        ui->pushButton_2->setText("GW");
}

void CreateMap::saveMapBack()
{
    int w=this->size().width();
    int h=this->size().height();
    QPixmap map(w, h);
    map.fill(Qt::transparent);

    QPainter painter1(this); // this 是 的指针
    painter1.begin(&map);
    painter1.setRenderHint(QPainter::Antialiasing, true);

    QLinearGradient linearGradient(0,0,w,h);
    //从点（100，150）开始到点（300，150）结束，确定一条直线
    linearGradient.setColorAt(0,Qt::darkGray);
    linearGradient.setColorAt(0.2,Qt::cyan);
    linearGradient.setColorAt(0.4,Qt::gray);
    linearGradient.setColorAt(0.6,Qt::darkCyan);
    linearGradient.setColorAt(0.8,Qt::lightGray);
    linearGradient.setColorAt(1,Qt::darkBlue);
    //将直线开始点设为0，终点设为1，然后分段设置颜色
    painter1.setBrush(linearGradient);
    painter1.drawRect(0,0,w,h);
    painter1.end();

    QPainter painter(this);
    painter.begin(&map);
    QPen pen;
    pen.setCapStyle(Qt::RoundCap);
    pen.setJoinStyle(Qt::RoundJoin);
    if(path.size()>1)
    {
        pen.setWidth(width);
        pen.setBrush(QColor(114,74,39));
        painter.setPen(pen);
        for(int i=1;i<path.size();i++)
        {
            if(global_width)
            {
                pen.setWidth(path[i].width);
                painter.setPen(pen);
            }
            painter.drawLine(path[i-1].sx,path[i-1].sy,path[i].sx,path[i].sy);
        }

        pen.setWidth(3);
        pen.setBrush(QColor(255,255,255));
        painter.setPen(pen);
        for(int i=1;i<path.size();i++)
            painter.drawLine(path[i-1].sx,path[i-1].sy,path[i].sx,path[i].sy);
    }
    if(endx!=-1&&endy!=-1)
    {
        pen.setWidth(5);
        pen.setBrush(Qt::darkCyan);
        painter.setPen(pen);
        painter.drawLine(endx,endy-width/2,endx,endy+width/2);
    }
    painter.end();
    map.save("d:\\mapback.png", "PNG");	//保存
}

void CreateMap::on_pushButton_clicked()
{
    int w=this->size().width();
    int h=this->size().height();
    QPixmap map(w, h);
    map.fill(QColor(255,255,0));

    QPainter painter(this); // this 是 的指针
    painter.begin(&map);
    painter.setRenderHint(QPainter::Antialiasing, true);
    QPen pen;
    pen.setWidth(width);
    pen.setBrush(Qt::black);
    pen.setCapStyle(Qt::RoundCap);
    pen.setJoinStyle(Qt::RoundJoin);
    painter.setPen(pen);
    if(path.size()>1)
    {
        for(int i=1;i<path.size();i++)
        {
            if(global_width)
            {
                pen.setWidth(path[i].width);
                painter.setPen(pen);
            }
            painter.drawLine(path[i-1].sx,path[i-1].sy,path[i].sx,path[i].sy);
        }
    }
    if(endx!=-1&&endy!=-1)
    {
        pen.setWidth(2);
        pen.setBrush(Qt::green);
        painter.setPen(pen);
        painter.drawLine(endx,endy-width/2,endx,endy+width/2);
    }
    painter.end();
    map.save("d:\\map.png", "PNG");	//保存
    saveMapBack();
}
